package net.akkz.dao.impl;

import net.akkz.dao.StudentDao;
import net.akkz.dbFactory.Session;
import net.akkz.exception.BuildClassError;
import net.akkz.exception.SQLExcuteException;
import net.akkz.model.Student;

public class StudentDaoImpl implements StudentDao
{

	@Override
	public void activeStudent(Session session, int studentId) throws SQLExcuteException
	{
		String sql = "update student set state = 1 where id = ?";
		
		Object[] values = new Object[1];
		values[0] = studentId;
		
		session.excute(sql, values);
	}

	@Override
	public Student getStudent(Session session, int studentId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from student where id = ?";

		return session.get(sql, studentId, Student.class);
	}
	
	@Override
	public Student getStudent(Session session, String studentName) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from student where name = ?";

		return session.get(sql, new Object[]{ studentName }, Student.class);
	}

	@Override
	public void updateStudent(Session session, Student student) throws SQLExcuteException
	{
		String sql = "update student set password=?,mail=? where id = ?";
		
		Object[] values = new Object[3];
		values[0] = student.getPassword();
		values[1] = student.getMail();
		values[2] = student.getId();
		
		session.excute(sql, values);
	}

	@Override
	public void register(Session session, Student student) throws SQLExcuteException
	{
		String sql = "insert into student(name, password, mail, state) values(?, ?, ?, ?)";
		
		Object[] values = new Object[4];
		values[0] = student.getName();
		values[1] = student.getPassword();
		values[2] = student.getMail();
		values[3] = student.getState();
		
		session.excute(sql, values);
	}

}
